<script setup>
import ImageAside from '@/components/image-aside.vue'
import ImageMain from '@/components/image-main.vue'
import { toast } from '@/utils/elnotification'
import { ref } from 'vue'

const dialogVisible = ref(false)

const callbackfun = ref(null)

const open = (callback = null) => {
  callbackfun.value = callback
  dialogVisible.value = true
}
const close = () => (dialogVisible.value = false)

defineExpose({
  open,
  close
})

const ImageAsideRef = ref(null)
//新增图片分类
const onOpenCreate = () => {
  ImageAsideRef.value.onOpenfromDrawer()
}

const ImageMainRef = ref(null)
const onChange = (id) => {
  ImageMainRef.value.loadData(id)
}

//上传图片
const onUploadImages = () => {
  ImageMainRef.value.showDrawer()
}

//拿到选中图片的数据
const props = defineProps({
  url: {
    type: [String, Array]
  },
  limit: {
    type: Number,
    default: 1
  },
  isShow: {
    type: Boolean,
    default: true
  }
})
let urls = []
const onChoose = (item) => {
  urls = item.map((i) => i.url)
}

//确认图片数据
const emit = defineEmits(['update:url'])
const submit = () => {
  let value = []
  if (props.limit == 1) {
    value = urls[0]
  } else {
    value = props.isShow ? [...props.url, ...urls] : [...urls]
    // console.log(value)
    if (value.length > props.limit) {
      let limit = props.isShow ? props.limit - props.url.length : props.limit
      return toast('最多还能选择' + limit + '张')
    }
  }

  if (value && props.isShow) {
    emit('update:url', value)
  }
  if (!props.isShow && typeof callbackfun.value === 'function') {
    callbackfun.value(value)
  }

  close()
}

const removeImage = (url) =>
  emit(
    'update:url',
    props.url.filter((u) => u != url)
  )
</script>

<template>
  <div v-if="url && isShow" class="mt-2">
    <el-avatar
      v-if="typeof url == 'string'"
      shape="square"
      :size="100"
      fit="cover"
      :src="url"
      class="w-[100px] h-[100px] rounded border mr-2"
    />
    <div v-else class="flex flex-wrap">
      <div class="relative mx-1 mb-2 w-[100px] h-[100px]" v-for="(item, index) in url" :key="index">
        <el-icon
          class="absolute right-[5px] top-[5px] cursor-pointer bg-white rounded-full"
          style="z-index: 10"
          @click="removeImage(item)"
          ><CircleClose
        /></el-icon>
        <el-image
          :src="item"
          fit="cover"
          class="w-[100px] h-[100px] rounded border mr-2"
        ></el-image>
      </div>
    </div>
  </div>
  <div v-if="isShow" class="choose-image-btn" @click="dialogVisible = true">
    <el-icon :size="25"><Plus /></el-icon>
  </div>

  <el-dialog title="选择图片" v-model="dialogVisible" width="80%" top="5vh">
    <el-container class="bg-white rounded" style="height: 70vh">
      <el-header class="image-header">
        <el-button type="primary" size="small" @click="onOpenCreate">新增图片分类</el-button>
        <el-button type="primary" size="small" @click="onUploadImages">上传图片</el-button>
      </el-header>
      <el-container>
        <ImageAside ref="ImageAsideRef" @change="onChange"></ImageAside>
        <ImageMain :limit="limit" open-choose ref="ImageMainRef" @choose="onChoose"></ImageMain>
      </el-container>
    </el-container>

    <template #footer>
      <span>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style>
.choose-image-btn {
  @apply w-[100px] h-[100px] rounded border flex justify-center items-center
    cursor-pointer hover:(bg-gray-100);
}
.image-header {
  border-bottom: 1px solid #eee;
  @apply flex items-center;
}
</style>
